<?php
/**
 * 类名称
 * User: 13651
 * Time: 2025/9/25 15:35
 */

namespace App\Services;

use App\Api\Controllers\LeshuaUnion\LeshuaBaseController as LeShuaUnionService;
use App\Extensions\BaseService;
use App\Repository\LeshuaUnionImgRepo;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class LeshuaUnionImgService  extends BaseService
{

    /**
     * 根据商户id获取信息
     * @param int $storeId
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function getByStoreIdService(string $storeId)
    {
        return LeshuaUnionImgRepo::getInstance()->getByStoreId($storeId);
    }

    public function uploadImgService(string $store_id,array $arr,array $leshuaConfig)
    {
        DB::beginTransaction();

        /**
         * 变量开关
         */
        $commit = false;
        $resultArr = [];
        foreach ($arr as $key=>&$value)
        {
            $img = '';
            try {
                //压缩图片
                $img_obj = \Intervention\Image\Facades\Image::make($value);
                $fileName = basename($value, PATHINFO_EXTENSION);
                $img = public_path() . '/upload/s_images/';
                if (!is_dir($img)) {
                    mkdir($img, 0777);
                }
                $img_obj->resize(800, 700);
                $img_obj->save($img.$fileName);
            } catch (\Exception $exception) {
                throw new \Exception( "图片压缩异常：".$exception->getMessage());
            }
            $value = $img.$fileName;
            /**
             * 获取文件扩展名称
             */
            $ext = pathinfo($value, PATHINFO_EXTENSION);
            $file = file_get_contents($value);
            $data = [
                'ext' => $ext,
                'picData'=>base64_encode($file),
            ];
            $result = LeShuaUnionService::getInstance()->uploadPic($leshuaConfig,$data);
            Log::info('乐刷联合支付上传图片返回报文'.$result);
            if (empty($result))
                throw new \Exception("乐刷图片上传异常！");

            $leshuaData = json_decode($result,true);
            if ($leshuaData['error_code']!='0')
                throw new \Exception($leshuaData['error_msg']);

            /**
             * 获取图片信息是否存在
             */
            $imgInfo = LeshuaUnionImgRepo::getInstance()->getByStoreId($store_id);
            if (!empty($imgInfo))
            {
                $id = $imgInfo->id;
            }else{
                $id = 0;
            }

            /**
             * 信息入库
             */
            $saveArr = [
                'store_id' => $store_id,
                "$key"=>$leshuaData['data']['path'],
            ];
            $saveRes = LeshuaUnionImgRepo::getInstance()->saveData($saveArr,$id);
            if (empty($saveRes))
            {
                DB::rollBack();
            }else{
                $commit = true;
                $resultArr["$key"] = $leshuaData['data']['path'];
            }
        }

        /**
         * 根据开关变量执行事务
         */
        if ($commit === true)
        {
            DB::commit();
        }
        return $resultArr;
    }
}